package com.zy.fee.roadpark.activity.np;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.zy.fee.roadpark.R;
import com.zy.fee.roadpark.config.Constants;
import com.zy.fee.roadpark.dao.NpNodeRecordLogic;
import com.zy.fee.roadpark.model.apimodel.APIM_NodeRecordInfo;
import com.zy.fee.roadpark.view.CommonDialog;
import com.zygk.library.base.BaseActivity;
import com.zygk.library.util.Convert;
import com.zygk.library.util.DateTimeUtil;
import com.zygk.library.util.HttpRequest;
import com.zygk.library.util.ToastUtil;

/**
 * 补缴单据详情
 * Created by qhj on 2018/9/14 0014.
 */

public class NpOverpayBillDetailActivity extends BaseActivity {
  public static final String INTENT_RECORD_ID = "INTENT_RECORD_ID";

  @BindView(R.id.lh_tv_title) TextView lhTvTitle;
  @BindView(R.id.tv_money) TextView tvMoney;
  @BindView(R.id.tv_money_fill) TextView tvFillMoney;
  @BindView(R.id.tv_carNumber) TextView tvCarNumber;
  @BindView(R.id.tv_lotName) TextView tvLotName;
  @BindView(R.id.tv_startTime) TextView tvStartTime;
  @BindView(R.id.tv_min) TextView tvMin;
  @BindView(R.id.iv_car) ImageView ivCar;

  private String nodeRecordID;
  private APIM_NodeRecordInfo m;

  @Override public void setContentView() {
    setContentView(R.layout.activity_np_overpay_bill_detail);
  }

  @Override public void init() {
    initData();
    initView();
    initListener();
  }

  private void initData() {
    nodeRecordID = getIntent().getStringExtra(INTENT_RECORD_ID);
  }

  private void initView() {
    lhTvTitle.setText("补缴单详情");

    nbnode_record_info();
  }

  private void initListener() {

  }

  private void nbnode_record_info() {
    NpNodeRecordLogic.escapeRecordInfo(mContext, nodeRecordID, new HttpRequest.HttpCallback() {
      @Override public void onStart() {
        showNoCancelPd();
      }

      @Override public void onSucceed(Object obj) {
        APIM_NodeRecordInfo result = (APIM_NodeRecordInfo) obj;
        m = result;
        tvMoney.setText(Convert.getMoneyString3(m.getMoney()));
        tvFillMoney.setText(Convert.getMoneyString3(m.getEscapeMoney()));
        tvCarNumber.setText(m.getCarNumber());
        tvLotName.setText(m.getLotName());
        tvStartTime.setText(m.getStartTime());
        tvMin.setText(DateTimeUtil.parkTimeInfo(m.getSeconds() / 60));
        imageManager.loadUrlImage(m.getPic(), ivCar);
      }

      @Override public void onFailed() {
        ToastUtil.showNetErrorMessage(mContext);
      }

      @Override public void onFinish() {
        dismissPd();
      }
    });
  }

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ButterKnife.bind(this);
  }

  @OnClick({ R.id.ll_back, R.id.rtv_receive, R.id.rtv_pay, R.id.iv_code }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.ll_back:
        finish();
        break;
      case R.id.rtv_receive:
        CommonDialog.showConfirmPayDialog(mContext, m.getCarNumber(), m.getEscapeMoney(),
            "取消", "确定", () -> {
              record_escape_pay_admin();
            }, () -> {
            });
        break;
      case R.id.iv_code:
      case R.id.rtv_pay:
        String url =
            "https://www.ychel.com/pay?state=carnum";
        CommonDialog.showQrCodeDialog(mContext, url);
        break;
    }
  }

  private void record_escape_pay_admin() {
    NpNodeRecordLogic.escape_pay_admin(mContext, preferencesHelper.userManager().getUserID(),
        nodeRecordID, m.getEscapeMoney(), new HttpRequest.HttpCallback() {
          @Override public void onStart() {
            showNoCancelPd();
          }

          @Override public void onSucceed(Object obj) {
            ToastUtil.showMessage(mContext, "已完成线下收款");
            sendBroadcast(new Intent(Constants.BROADCAST_REFRESH_OVERPAY_BILL_LIST));
            finish();
          }

          @Override public void onFailed() {
            ToastUtil.showNetErrorMessage(mContext);
          }

          @Override public void onFinish() {
            dismissPd();
          }
        });
  }
}
